package com.neusoft.work.service.impl;

import java.util.List;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.neusoft.work.dao.CareLevelProjectDao;
import com.neusoft.work.dao.CareProjectDao;
import com.neusoft.work.po.CareLevelProject;
import com.neusoft.work.po.CareProject;
import com.neusoft.work.po.PageResponseBean;
import com.neusoft.work.po.ResponseBean;
import com.neusoft.work.service.CareLevelProjectService;

@Service
public class CareLevelProjectServiceImpl implements CareLevelProjectService {

    @Autowired
    private CareLevelProjectDao clpMapper;
    @Autowired
    private CareProjectDao projectMapper;

    @Override
    public PageResponseBean<List<CareProject>> getProjectsByLevel(Integer careLevelId) {
        List<CareLevelProject> list = clpMapper.selectList(
            new QueryWrapper<CareLevelProject>().eq("careLevelId", careLevelId)
        );
        List<Integer> ids = list.stream()
            .map(CareLevelProject::getCareProjectId)
            .collect(Collectors.toList());
        List<CareProject> projects = ids.isEmpty() ? List.of() : projectMapper.selectBatchIds(ids);
        PageResponseBean<List<CareProject>> resp = new PageResponseBean<>();
        resp.setData(projects);
        resp.setTotal((long) projects.size());
        return resp;
    }

    @Override
    public ResponseBean<Integer> addCareLevelProject(CareLevelProject clp) {
        int rows = clpMapper.insert(clp);
        ResponseBean<Integer> resp = new ResponseBean<>();
        resp.setData(clp.getId());
        resp.setStatus(rows > 0 ? 200 : 500);
        resp.setMsg(rows > 0 ? "添加成功" : "添加失败");
        return resp;
    }

    @Override
    public ResponseBean<Integer> deleteCareLevelProject(Integer careLevelId, Integer careProjectId) {
        int rows = clpMapper.delete(
            new QueryWrapper<CareLevelProject>()
                .eq("careLevelId", careLevelId)
                .eq("careProjectId", careProjectId)
        );
        ResponseBean<Integer> resp = new ResponseBean<>();
        resp.setData(rows);
        resp.setStatus(rows > 0 ? 200 : 500);
        resp.setMsg(rows > 0 ? "删除成功" : "删除失败");
        return resp;
    }
}